package com.qxbb.report.controller;


import com.qxbb.report.Enum.LoginUserType;
import com.qxbb.report.bo.LoginUser;
import com.qxbb.report.dao.ChainStoreMapper;
import com.qxbb.report.pojo.ChainStore;
import com.qxbb.report.service.ChainUserInforService;
import com.qxbb.report.utils.StringUtils;
import com.qxbb.report.utils.http.HttpResult;
import com.qxbb.report.vo.ChainStoreVO;
import com.qxbb.report.vo.UserInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * @version 1.0
 * @author: pebble
 * @data 2023/9/29 15:30
 */
@RequestMapping
@RestController
@CrossOrigin

public class LoginController {

    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ChainUserInforService chainUserInforService;
    @Autowired
    private ChainStoreMapper chainStoreMapper;


    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/login")
    public HttpResult login(@RequestBody LoginUser loginUser) {
        List<String> list  = new ArrayList<>();
        logger.info(this.getClass() + ".login 进入用户登录方法:{}", loginUser);
        if (StringUtils.isNotNull(loginUser)) {
            UserInfoVO user = null;
            //总部用户
            if (loginUser.getIsChain() == LoginUserType.HEADQUARTERS.getNumber()) {
                Boolean nameIsNull = chainUserInforService.selectUserName(loginUser.getUserName(),loginUser.getIsChain());
                if (nameIsNull) {
                    user = chainUserInforService.selectUser(loginUser);
                    if (StringUtils.isNull(user)) {
                        return HttpResult.ok("账号密码错误,请重新输入！");
                    } else {
                        return HttpResult.ok(user);
                    }
                }else {
                    return HttpResult.ok("此总部用户不存在,请重新输入！");
                }

            } else { // 连锁店登录用户;
                Boolean  isNull = chainUserInforService.selectUserName(loginUser.getUserName(),loginUser.getIsChain());
                if (isNull){
                    user = chainUserInforService.selectUser(loginUser);
                    if (StringUtils.isNull(user)){
                        return HttpResult.ok("账号密码错误,请重新输入！");
                    }else {
                        ChainStore chainStore = chainStoreMapper.selectByStoreId(user.getChainId());
                        if (StringUtils.isNull(chainStore)){
                            return HttpResult.ok("此账号关联连锁店已停用,无法登录！");
                        }else {
                            return HttpResult.ok(user);
                        }
                    }
                }else {
                    return HttpResult.ok("此连锁店用户不存在,请重新输入！");
                }
            }
        } else {
            return HttpResult.ok("登录用户信息为空！");
        }
    }


    /**
     * 查询用户是否拥有管理员和总部督导的权限
     *
     * @return
     */
    @PostMapping("/userInfo/{userId}/{userType}")
    public HttpResult selectUserIsAdmin(@PathVariable("userId") Integer userId, @PathVariable("userType") Integer userType) {
        logger.info(this.getClass() + ".selectUserIsAdmin 查询用户是否拥有管理员和总部督导的权限,用户id:{},用户类型:{}", userId,userType);
        return HttpResult.ok(chainUserInforService.selectUserIsAdmin(userId, userType));
    }


    /**
     * 查询登录用户角色信息
     * @return
     */
    @GetMapping("/role/{userId}/{userType}")
    public HttpResult selectUserRole(@PathVariable("userId") Integer userId, @PathVariable("userType") Integer userType) {
        logger.info(this.getClass() + ".selectUserRole 查询登录用户角色信息,用户id:{},用户类型:{}", userId,userType);
        return HttpResult.ok(chainUserInforService.selectUserRole(userId, userType));
    }


    @GetMapping("/img")
    @ResponseBody
    public String getImg(HttpServletRequest request){
        String  path = "C:\\Users\\Administrator\\Desktop\\work\\e.png";
        return "http://"+request.getRemoteHost()+":"+request.getRemotePort()+"/"+path;
    }

}
